import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'
// const Register =
// const Login =
// const Layout =
// const Detail =
// const Article =
// const Collect =
// const Like =
// const User =

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/article' },
  {
    path: '/register',
    component: () =>
      import('@/views/Register')
  },
  {
    path: '/login',
    component: () =>
      import('@/views/Login')
  },
  {
    path: '/detail/:id',
    component: () =>
      import('@/views/Detail')
  },
  {
    path: '/',
    component: () =>
      import('@/views/Layout'),
    children: [{
      path: '/article',
      component: () =>
        import('@/views/Article')
    },
    {
      path: '/collect',
      component: () =>
        import('@/views/Collect')
    },
    {
      path: '/like',
      component: () =>
        import('@/views/Like')
    },
    {
      path: '/user',
      component: () =>
        import('@/views/User')
    }
    ]
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      Toast.fail('您还未登录，请登录')
      next('/login')
    }
  }
})

export default router
